package collection.mymap;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

public class HashMapReverse {
    public static void main(String[] args) {
        //Map后面不写
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,100);
        map.put(2,120);
        map.put(4,140);
        map.put(6,160);
        map.put(7,170);

        //1 原始hashmap结构
        System.out.println(map);



        //1.1 遍历键
        for(Integer key: map.keySet()){
            System.out.println("Key = " + key);
        }

        //1.2 遍历值
        for(Integer value: map.values()){
            System.out.println("Value = " + value);
        }



        //1.3 整体遍历
        for(Map.Entry<Integer,Integer> entry: map.entrySet()) {
            System.out.println("Key = " + entry.getKey() + " Value = " + entry.getValue());
        }
        System.out.println();


        //1.4 整体遍历，main.java.lambda
        System.out.println("使用lambda");
        map.forEach((k,v) -> System.out.println("key: " + k + " value:" + v));
        System.out.println();



        //1.5 遍历，main.java.stream
        System.out.println("使用stream");
        map.entrySet().stream().forEach((entry)-> System.out.println("Key = " + entry.getKey() + " Value = " + entry.getValue()));
        System.out.println("使用stream，输出entry");
//        map.entrySet().main.java.stream().max();

        String[] strings = {"aicm","sso","ssf","Abca"};
//
//        int longestStringLengthStartingWithA
//                = strings.main.java.stream()
//                .filter(s -> s.startsWith("A"))
//                .mapToInt(main.java.String::length)
//                .max();

        System.out.println();

        //遍历出最大的
//        map.entrySet().main.java.stream().forEach((entry)-> System.out.println()).;

        //2
        Map<Integer,Integer> map2 = new HashMap<>();

//      用过滤出的hashmap元素生成新的Map，并赋给另一个map
        map2 = map.entrySet().stream().filter(e -> e.getKey() >= 3)
                .collect(Collectors.toMap(Entry::getKey, Entry::getValue
                ));

        map2.forEach((k,v)-> System.out.println("key: " + k + " value:" + v));
        System.out.println();
        System.out.println(map2);

    }

}
